package com.cn.service.Impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cn.mapper.CategoryMapper;
import com.cn.pojo.Category;
import com.cn.pojo.Dish;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.mapper.DishMapper;
import com.cn.service.DishService;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Resource
    private CategoryMapper categoryMapper;


    @Override
    public Page<Dish> selectAllByPid(Page page, String name, Integer status, Long pid, Long id) {
        List<Dish> pageList = this.dishMapper.selectAllByPid(page, name, status, pid, id);
        page.setRecords(pageList);
        return page;
    }

    /***
     * 废弃
     * @param page
     * @param name
     * @param status
     * @param categoryId
     * @return
     */

    @Override
    public Page<Dish> selectAllByDish(Page page, String name, Integer status, Long categoryId) {
        List<Dish> pageList = this.dishMapper.selectAllByPid(page, name, status, categoryId, categoryId);
        page.setRecords(pageList);
        return page;

    }

    @Override
    public Integer getInsert(Dish dish) {
        Category category1 = this.categoryMapper.selectById(dish.getCategoryId());
        if (category1.getLevel() != 2) {
            return 0;
        }
        return this.baseMapper.insert(dish);
    }

    @Override
    public Integer getUpdate(Dish updatedish) {
        return this.baseMapper.updateById(updatedish);
    }

    @Override
    public Integer getDelete(Long id) {
        return this.baseMapper.deleteById(id);
    }

    @Override
    public Dish selectById(Long id) {
        return dishMapper.selectById(id);
    }

    @Override
    public Category selectBy(Long id) {
        return this.categoryMapper.selectById(id);
    }

}

